import analysis
import jsTool
import search
import Image

style =  ' width = "7" height = "7"  '

def getPicMap(filename,subPicNum):
    cSpace = jsTool.j2o('colorSpace.json')
    bitmap = analysis.analysisPic(filename,subPicNum)
    print 'bitmap size: ', len(bitmap[0]),len(bitmap)
    picmap=[]

    i=0
    for y in bitmap:
        print i , 'in' , len(bitmap)
        i+=1
        linemap =[]
        for x in y:
            pic = search.getPic(x,cSpace)
            #print pic
            linemap.append(pic)
        picmap.append(linemap)
    
    return picmap

def htmlGenerate(pMap):


    fname2link= jsTool.j2o('fname2link.json')
    
    #print html

    x=len(pMap[0])
    y=len(pMap)
    print x,y
    zoom=6
    unit = 60

    'str'
    width = 'width = "'+str(x*unit/zoom)+'" height = "'+str(y*unit/zoom)+'"'
    
    
    im=Image.new('RGB', (x*unit, y*unit))

    
    length = len(pMap)
    yy=0
    miss= ''

    
    css='<style>.imgSize {width:70px;height:70px;}</style>'
    
    html='<html xmlns="http://www.w3.org/1999/xhtml"><head>\n<title>Nilin \'s douban summary </title>'
    html+='<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>\n'
    html+="\n<script>\nfunction fun(tt,event){document.getElementById('showPane').src=tt.picsrc;document.getElementById('showPaneDiv').style.left=event.clientX+document.body.scrollLeft-document.body.clientLeft+'px';document.getElementById('showPaneDiv').style.top=event.clientY+document.body.scrollTop-document.body.clientTop+50+'px';}\n</script>\n"
    html+='<img src="bigpic3.jpg" border="0" usemap="#picmap" alt="nilin" '+width+' />\n'
    html+='<map name="picmap" id="planetmap">\n'
    html+='\n\n<div id="showPaneDiv" border = "2" style="position:absolute;right:0;top:0;width:20%;height:20%;border:2px;">\n<img  class="imgSize" id="showPane" src="http://img3.douban.com/view/photo/thumb/public/p782604158.jpg" /></div>\n'
    
    for y in pMap:
        xx=0
        print yy , '/', length
        #print '<tr>'
        for x in y :
            #print x
            picName = x
            #print picName
            
            #paste pics
            try :
                pic = Image.open(x).resize((unit,unit));
                im.paste(pic,(xx*unit,yy*unit))
            except Exception ,e:
                pass
            
            href = x
            #print x
            subNum=x.split('[')[0][-7:];
            title = x.split('[')[1].split(']')[0]

            coords = '"'+str(xx*unit/zoom)+','+str(yy*unit/zoom)+','+str((xx+1)*unit/zoom)+','+str((yy+1)*unit/zoom)+'"'
            td = '\n\t<area  shape="rectangle" coords='+coords+' href = "http://douban.com/subject/' 
            try:
                link= fname2link[picName]
            except :
                miss+=picName+'\n'
                link = 'empty'
                link = 'http://avatar.csdn.net/B/4/E/1_ksrs.jpg'
            #print picName
            #print link
            td+=subNum + '/" target="_blank"'
            td += ' alt="'+title+'" title = "'+title
            td+= '" border = "0"  onmousemove="fun(this,event);"    picsrc = "'+link +'"  />'
            html+=td
            #print td
            xx+=1
        #print '</tr>'
        yy+=1


    #im.show()
    im.save('bigpic3.jpg')

    html += '</map>'
    #print '</table>'+'<html>'
    print len(pMap[0])
    #print miss

    print len(y),":",len(pMap)

    print fname2link.keys()[0]
    return html

def main(picname,subPicNum):
    pMap = getPicMap(picname,subPicNum)
    #print pMap
    html=htmlGenerate(pMap)
    print isinstance(html,unicode)
    
    for c in ('utf-8','gbk','gb2312','SHIFT_JIS'):
        try :
            html=html.encode(c)
            f=open(picname+'.area.html','w')
            f.write(html)
            f.close()
            print c ,' success'
            break
        except :
            print c ,' error'

if __name__=='__main__':
    main('nilin.jpg',10000)

    #size=1000*1000 ,divide into 10*10pics
    '''
    x,y=1000,1000
    l=10       
    size = (x/l,y/l)
    pic = Image.open('nilin.jpg').resize(size);
    im=Image.new('RGB', (x, y))  
    for i in range(0,l):
        for j in range(0,l):
            im.paste(pic,(x*i/l,y*j/l))
    im.show()
    '''

             
